package com.jvtd.baseProject.base;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.jvtd.base.JvtdMvpActivity;
import com.jvtd.baseProject.R;
import com.jvtd.baseProject.app.MyApplication;
import com.jvtd.baseProject.di.component.DaggerActivityComponent;
import com.jvtd.baseProject.di.module.ActivityModule;
import com.jvtd.baseProject.constants.EventCode;
import com.jvtd.baseProject.di.component.ActivityComponent;
import com.jvtd.baseProject.rxjava.JvtdRxSchedulers;
import com.jvtd.baseProject.ui.login.LoginActivity;
import com.jvtd.baseProject.ui.main.MainActivity;
import com.jvtd.eventBus.EventCenter;
import com.jvtd.widget.dialog.JvtdAlertDialog;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseMvpActivity extends JvtdMvpActivity implements MvpView {
    @Inject
    BasePresenter<MvpView> mPresenter;

    private Unbinder mUnBinder;
    private JvtdAlertDialog mAlertDialog;

    @Override
    protected void bindView() {
        mUnBinder = ButterKnife.bind(this);
    }

    @Override
    protected void onViewCreated(Bundle savedInstanceState) {
        super.onViewCreated(savedInstanceState);
        initInject();
    }

    /**
     * activity degger2 如果未使用 @Inject注解  实现后里面可以不写任何方法
     *
     * @author Chenlei
     * created at 2018/9/25
     **/
    protected ActivityComponent getActivityComponent() {
        return DaggerActivityComponent.builder()
                .applicationComponent(((MyApplication) getApplication()).getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .build();
    }

    protected abstract void initInject();

    /**
     * 检测是否需要登录
     *
     * @author Chenlei
     * created at 2018/9/25
     **/
    @Override
    public void isLogin(boolean isLogin) {
        if (isLogin)
            loginGranted();
        else
            new JvtdAlertDialog().setTitle(getString(R.string.warm_prompt_title))
                    .setMessage(getString(R.string.you_are_not_login_please_login_title))
                    .setOnClickListener((position, inputText) -> {
                        switch (position) {
                            case JvtdAlertDialog.RIGHT://需要跳转登录界面
                                startActivity(LoginActivity.getIntent(mContext));
                                break;
                        }
                    })
                    .show(getSupportFragmentManager());
    }

    /**
     * 已登录回调
     */
    protected void loginGranted() {

    }

    /**
     * 退出登录成功
     *
     * @author Chenlei
     * created at 2018/9/25
     **/
    @Override
    public void logoutSuccess() {

    }

    /**
     * 必须退出登录
     * created at 2018/9/25
     **/
    @Override
    public void mustLogout() {
        EventBus.getDefault().post(new EventCenter<>(getString(R.string.need_login_tips), EventCode.SSO));
    }

    @Override
    public int mustLogoutCode() {
        return JvtdRxSchedulers.CODE_NEED_LOGIN;
    }

    @Override
    public void activityOnEvent(EventCenter eventCenter) {
        super.activityOnEvent(eventCenter);
        switch (eventCenter.getEventCode()){
            case EventCode.SSO:
                if (mPresenter != null && mPresenter.getDbManager().isLogin())
                    mPresenter.logout(false);// 登出
                showLoginStatusErrorDialog((String)eventCenter.getData());
                break;
        }
    }

    //显示登录状态异常弹窗
    private void showLoginStatusErrorDialog(String tips)
    {
        // 弹出登出Dialog
        if (TextUtils.isEmpty(tips))
            tips = getString(R.string.dialog_login_status_error);
        if (mAlertDialog != null) return;
        mAlertDialog = new JvtdAlertDialog();
        mAlertDialog.setTitle(tips)
                .setShowContentLayout(false)
                .setShowLeftBtn(false)
                .setCanCancelable(false)
                .setKeyBackEnabled(false)
                .setOnClickListener((isLeft, inputText) ->
                {
                    if (mPresenter != null && mPresenter.getDbManager().isLogin())
                        mPresenter.logout(false);// 登出
                    mAlertDialog = null;
                    int[] flags = new int[]{Intent.FLAG_ACTIVITY_NEW_TASK, Intent.FLAG_ACTIVITY_SINGLE_TOP};
                    Intent intent = MainActivity.getIntent(mContext);
                    for (int flag : flags)
                        intent.addFlags(flag);
                    startActivity(intent);
                    EventBus.getDefault().post(new EventCenter<>(EventCode.SHOW_MAIN));
                })
                .show(getSupportFragmentManager());
    }
}
